/**
 * Created by bgy on 2016/12/6.
 * 我的水果

 get input value based on https://facebook.github.io/react/docs/uncontrolled-components.html and refer to the summary:
 http://goshakkk.name/controlled-vs-uncontrolled-inputs-react/

 */
var React = require("react");
var ReactDOM = require("react-dom");
var AttachmentMgt = require('../common/AttachmentMgtNew');
var CitiesSelection = require('../common/CitiesSelection');
var FruitSelection = require('../common/FruitSelection');
var HeaderTitle = require('../common/HeaderTitle');
var FruitSelectionPage = require('../common/FruitSelectionPage');
var Navigation = require('react-router');
var filemgt_server_url = "http://a8-domain.pagoda.com.cn:20101";
var Router = require('react-router');
var Link = Router.Link;
class MyFruitDetail extends  React.Component{
  constructor(props){
    console.log(props);
    super(props);
    this.state = {
      myGoods:[]
    }
  }
  getInitialState(){
  return {
    selectedFruit: "",
    spec: "",
    packagingWay: "",
    unitPrice: "",
    supplyAmt: "",
    startValidDate: "",
    supplyDuration: "",
    sendAddress: "",
    desc: "",
    files: [],
    mode:"normal"
  };
}
  handleCommit(){
  var post_data = $.map(this.refs.files.save(),function(item,idx){
    return {
      appKey:'trade',
      ownType:'myfruit',
      ownCode:'00001',
      fileUseCode:'dfasdf',
      fileUseLabel:item.fileUseLabel,
      fileKey:item.fileKey,
      bucketname:item.bucketname,
    };
  });
  $.post({
    url:filemgt_server_url + '/fileMgr/updateFilesByFileUseCode',
    data:JSON.stringify(
      {
        appKey:'trade',
        ownType:'myfruit',
        ownCode:'00001',
        fileUseCode:'dfasdf',
        fmFiles:post_data
      }
    ),
    contentType : 'application/json',
    dataType: 'json',
    success:function (data) {
      console.log("get the success for update");
    }
  });
}
  componentDidMount(){
  var myself = this;
  var state = window.sessionStorage.getItem('myFruitDetail.temp');
  console.log("asfsdfads");
  if(state != null){

  }
  $.post({
    url:filemgt_server_url + '/fileMgr/queryFiles',
    data:JSON.stringify(
      {
        appKey:'trade',
        ownType:'myfruit',
        ownCode:'00001',
        fileUseCode:'dfasdf',
      }
    ),
    contentType : 'application/json',
    dataType: 'json',
    success:function (data) {
      myself.refs.files.setState({files:data.data})
    }
  });
}
  componentWillUnmount(){
  //在页面消失前，收集所有信息
  console.log("_MyFruitDetail componentWillUnmount");
  /*this.state.selectedFruit = this.selectedFruit.getValue().name;
   this.state.spec = this.refs.spec.value;
   this.state.packagingWay = this.refs.packagingWay.value;
   this.state.unitPrice = this.refs.unitPrice.value;
   this.state.supplyAmt = this.refs.supplyAmt.value;
   this.state.startValidDate = this.refs.startValidDate.value;
   this.state.supplyDuration = this.refs.supplyDuration.value;
   this.state.sendAddress = this.refs.CitiesSelection.getValue();
   this.state.desc = this.refs.desc.value;
   this.state.files = this.refs.AttachmentMgt.save();*/

  window.sessionStorage.setItem("myFruitDetail.temp",this.state);
}
  jumpToFruitSelection(){
  //this.props.history.push("/fruitSelection");
  //window.location.href = "/fruitSelection";
  //this.props.router.push('/fruitSelection');
  this.setState({mode:'fruitSelection'});
}
  fruitSelctionCallback(){
  this.setState({mode:"normal"});
}
  render(){
  var tips = [];

  // <div className='form-item'>
  // <label>水果名称</label><FruitSelection value={this.state.selectedFruit} ref='selectedFruit'></FruitSelection>
  //</div>
  {
    var panelVisible = {
      normalPanel:"block",
      fruitSelection:"none",
      citiesSelection:"none"
    };
    if(this.state.mode == 'normal'){
      panelVisible.normalPanel = 'block';
      panelVisible.fruitSelection = "none";
    }
    if(this.state.mode == 'fruitSelection'){
      panelVisible.normalPanel = 'none';
      panelVisible.fruitSelection = "block";
    }
    return(
      <div>
        <div style={{display:panelVisible.normalPanel}}>
          <div className='form'>
            <div className='form-item'>
              <label>水果名称</label><input ref='selectedFruit' onFocus={this.jumpToFruitSelection}></input>
            </div>
            <Link to="/fruitSelection" >跳转</Link>
            <div className='form-item'>
              <label>规格</label><input  ref='spec'/>
            </div>
            <div className='form-item'>
              <label>包装</label><input  ref='packagingWay'/>
            </div>
            <div className='form-item'>
              <label>单价</label><input placeholder='裸果价' ref='unitPrice'></input>
            </div>
            <div className='form-item'>
              <label>供应量</label><input ref='supplyAmt'></input>
            </div>
            <div className='form-item'>
              <label>供货起始时间</label><input type="date" ref='startValidDate'></input>
            </div>
            <div className='form-item'>
              <label>供货期</label><input list="supply-duration" ref='supplyDuration'></input><datalist id='supply-duration'>
              <option value="半个月"/>
              <option value="一个月"/>
              <option value="两个月"/>
              <option value="三个月"/>
              <option value="半年"/>
              <option value="全年"/></datalist>
            </div>
            <div className='form-item'>
              <label>发货地点</label><CitiesSelection ref='sendAddress'></CitiesSelection>
            </div>
            <div className='form-item'>
              <label>物流方式</label><input ref='logWay'></input>
            </div>
            <div className='form-item'>
              <label>果品描述</label><input type='textarea' ref='desc'></input>
            </div>
            <div className='form-item pic'>
              <label>上传图片说明</label>
              <AttachmentMgt ref='files'></AttachmentMgt>
            </div>

            <button className="button button-pill button-primary btn-green btn-long" ref="loginBtn">确认发布</button>
          </div>
        </div>
        <div style={{display:panelVisible.fruitSelection}}>
          <FruitSelectionPage callback={this.fruitSelctionCallback.bind(this)}></FruitSelectionPage>
        </div>
      </div>
    );
  }
}
}
export default MyFruitDetail;

